﻿// File_Login_Dlg.cpp: 实现文件
//

#include "pch.h"
#include "SakerProject.h"
#include "File_Login_Dlg.h"
#include "afxdialogex.h"

#include "xxWidgetControl.h"


// CFile_Login_Dlg 对话框
CFile_Login_Dlg * m_File_Login;

IMPLEMENT_DYNAMIC(CFile_Login_Dlg, CDialogEx)

CFile_Login_Dlg::CFile_Login_Dlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_File_Login, pParent)
{

}

CFile_Login_Dlg::~CFile_Login_Dlg()
{
}

void CFile_Login_Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CFile_Login_Dlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_Login, &CFile_Login_Dlg::OnBnClickedButtonLogin)
	ON_BN_CLICKED(IDC_BUTTON_Logout, &CFile_Login_Dlg::OnBnClickedButtonLogout)
END_MESSAGE_MAP()


// CFile_Login_Dlg 消息处理程序


BOOL CFile_Login_Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	ShowUserInfo();

	return TRUE;
}

void CFile_Login_Dlg::ShowUserInfo() {
	SetDlgItemText(IDC_SHOW_CurUserName, mmFile_Login.CurUser.UserName);
	SetDlgItemText(IDC_SHOW_CurPermission, mmFile_Login.CurUser.Description());
}

void CFile_Login_Dlg::OnBnClickedButtonLogin()
{
	CString UserName = xx::ReadWidget(this, IDC_EDIT_UserName);
	CString PassWord = xx::ReadWidget(this, IDC_EDIT_PassWord);
	if (UserName == L"") {
		AfxMessageBox(L"请输入用户名", MB_OK);
		Flag_LoggedIn = false;
		return;
	}
	if (PassWord == L"") {
		AfxMessageBox(L"请输入密码", MB_OK);
		Flag_LoggedIn = false;
		return;
	}
	bool CheckValidity = false;
	for (int i = 0; i < iiFILE_LOGIN_AllUser_Num; i++) {
		if (mmFile_Login.AllUsers[i].UserName == UserName && mmFile_Login.AllUsers[i].PassWord == PassWord) {
			mmFile_Login.CurUser = mmFile_Login.AllUsers[i];
			CheckValidity = TRUE;
			break;
		}
	}
	if (CheckValidity == false) {
		AfxMessageBox(L"请输入正确的用户名和密码", MB_OK);
		Flag_LoggedIn = false;
		return;
	}
	ShowUserInfo();

	Flag_LoggedIn = TRUE;
}


void CFile_Login_Dlg::OnBnClickedButtonLogout()
{
	mmFile_Login.CurUser = mmFile_Login.DefaultUsers[ooPermission_Guest];
	ShowUserInfo();
}


BOOL CFile_Login_Dlg::PreTranslateMessage(MSG* pMsg)
{
	int MsgMessage = pMsg->message;
	int MsgParam = pMsg->wParam;
	if (MsgMessage == WM_KEYDOWN && MsgParam == VK_RETURN) {
		OnBnClickedButtonLogin();
		if (Flag_LoggedIn == TRUE) {
			OnCancel();
		}
		return TRUE;
	}

	return CDialogEx::PreTranslateMessage(pMsg);
}
